package com.ruoyi.project.hcy.task.service;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.hcy.administrations.domain.Crew;
import com.ruoyi.project.hcy.task.domain.Task;
import com.ruoyi.project.hcy.task.domain.TaskPostpone;
import com.ruoyi.project.hcy.task.mapper.TaskMapper;
import com.ruoyi.project.hcy.task.mapper.TaskPostponeMapper;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 任务延期 服务层处理
 */
@Service
public class TaskPostponeServiceImpl implements ITaskPostponeService {

    @Autowired
    TaskPostponeMapper taskPostponeMapper;

    @Autowired
    TaskMapper taskMapper;

    @Override
    public List<TaskPostpone> selectTaskPostponeList(TaskPostpone taskPostpone) {
        return taskPostponeMapper.selectTaskPostponeList(taskPostpone);
    }

    @Override
    public int insertPostpone(TaskPostpone taskPostpone, Long taskId, String taskEndTimeStr, Long userId) {
        Date date = strToDate(taskEndTimeStr);
        Task task = taskMapper.selectTaskById(taskId);
        taskPostpone.setTaskEndTime(date);
        Crew user = taskMapper.selectCrewById(userId);
        taskPostpone.setUserCrew(user);
        taskPostpone.setTask(task);
        return taskPostponeMapper.insertPostpone(taskPostpone);
    }

    @Override
    public List<Task> selectTaskList() {
        Task task = new Task();
        String loginName = ShiroUtils.getLoginName();
        User user = taskMapper.selectLoginUser(loginName);
        task.setCreatorUser(user);
        return taskMapper.selectTaskList(task);
    }

    @Override
    public int deleteTaskPostponeByIds(String ids) {
        Long[] postponeIds = Convert.toLongArray(ids);
        return taskPostponeMapper.deleteTaskPostponeByIds(postponeIds);
    }

    @Override
    public TaskPostpone selectTaskPostponeById(Long postponeId) {
        return taskPostponeMapper.selectTaskPostponeById(postponeId);
    }

    @Override
    public int updateTaskPostpone(TaskPostpone taskPostpone, String taskEndTimeStr, Long taskId) {

        Date taskEndTime = strToDate(taskEndTimeStr);
        taskPostpone.setTaskEndTime(taskEndTime);
        taskPostpone.setVerifyTime(new Date());
        if (taskId != null) {
            String loginName = ShiroUtils.getLoginName();
            User user = taskMapper.selectLoginUser(loginName);
            taskPostpone.setVerifierUser(user);
            if ("1".equals(taskPostpone.getPostponeStatus())) {
                Task task = new Task();
                task.setTaskEndTime(taskEndTime);
                task.setTaskId(taskId);
                taskMapper.updateTask(task);
            }
        }
        return taskPostponeMapper.updateTaskPostpone(taskPostpone);
    }

    @Override
    public Crew selectCrewOfTask(Long taskId) {
        Task task = taskMapper.selectTaskById(taskId);
        Crew crew = task.getExecutorCrew();
        return crew;
    }

    private Date strToDate(String dateFormatterStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(dateFormatterStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}
